package com.qire.manhua.controller;

import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

import com.qire.common.support.CustomLoadDialog;
import com.qire.common.support.base.EventController;
import com.qire.manhua.R;
import com.qire.manhua.activity.AccountsAssociatedAddActivity;
import com.qire.manhua.databinding.ActivityAccountsAssociatedAddBinding;
import com.qire.manhua.model.viewModel.impl.AccountsAssociatedAddViewModel;

import androidx.annotation.NonNull;

public class AccountsAssociatedAddController extends EventController<AccountsAssociatedAddActivity> {

    private AccountsAssociatedAddViewModel viewModel;

    public AccountsAssociatedAddController(@NonNull AccountsAssociatedAddActivity owner) {
        super(owner);
    }

    @Override
    protected void bindView() {
        ActivityAccountsAssociatedAddBinding viewDataBinder = owner.viewDataBinder();
        viewDataBinder.setAccountsAssociatedAddController(this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(AccountsAssociatedAddViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**************************************************************************************************************/

    public void backBtnOnClick(View view) {
        owner.finish();
    }

    /**获取验证码*/
    public void fetchVerifyCodeBtnOnClick(View view) {
        viewModel.fetchVerifyCode();
    }

    /**验证码验证按钮事件响应*/
    public void verifyBtnOnClick(View view) {
        viewModel.checkVerificationCode();
    }

    public void verifyCodeOnFocusChange(View view, boolean hasFocus) {
        if (hasFocus) {
            view.setBackgroundResource(R.drawable.app_common_edit_verify_code_n);
        } else {
            view.setBackgroundResource(R.drawable.app_common_edit_verify_code_s);
        }
    }
    //解决光标在最前面的位置导致退格键失效的问题
    public boolean verifyCodeOnKey(View view, int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_DEL) {
            EditText editText = (EditText) view;
            int textLength = editText.getText().toString().length();
            if(textLength > 0) {
                editText.setSelection(textLength);
            } else {
                editText.setText("");
            }
        }
        return false;
    }

    public void gotoPhonePanelBtnOnClick(View view) {
        viewModel.gotoPanel(1);
    }

    public void gotoWeChatPanelBtnOnClick(View view) {
        CustomLoadDialog.show(owner, "请稍等", true);
        viewModel.accountVerifyByWeChat();
    }
    public void gotoQQPanelBtnOnClick(View view) {
        CustomLoadDialog.show(owner, "请稍等", true);
        viewModel.accountVerifyByQQ();
    }

    public void phoneAuthBtnOnClick(View view) {
        viewModel.accountVerifyByPhone();
    }

    public void confirmAssociatedBtnOnClick(View view) {
        viewModel.accountsAssociatedAdd();
    }

    public void cancelAssociatedBtnOnClick(View view) {
        viewModel.gotoPanel(0);
    }

}
